
export type ProgressHandle = (complateCount: number, totalCount: number) => void
export type ComplateHandle<T> = (err: Error, res: T) => void
export default class Utils {
    
    public static getRandomLv():number {
        return Math.floor(Math.random() * 1080);
    }
    
    public static getAnimalIndex(lv) {
        return Math.floor(lv / 180);
    }
   
    public static strfmt(fmt: string, ...args: any[]) {
        return fmt.replace(/\{(\d+)\}/g, (match: string, argIndex: number) => {
            return args[argIndex] || match;
        });
    }
    /** 时间格式化 "00:00"*/
    public static formatToTime2(num) {
        num = num || 0;
        num = Math.floor(num);
        num = num < 1 ? 0 : num;
        let s = num % 60;
        let d = Math.floor(num / 60)
        let m = d % 60;
        d = Math.floor(d / 60)
        let date = "";
        date += m == 0 ? "00" : m < 10 ? "0" + m : m;
        date += ":";
        date += s == 0 ? "00" : s < 10 ? "0" + s : s;

        return date;
    }
    public static formatToSecond(data) {
        var time = Number(data);
        var h = Math.floor(time / 3600);
        var m = Math.floor((time % 3600) / 60);
        var s = time % 3600 % 60;
        var hh = h < 10 ? "0" + h : h;
        var mm = m < 10 ? "0" + m : m;
        var ss = s < 10 ? "0" + s : s;
        return hh + ":" + mm + ":" + ss;
    }
    public static timeString(date: Date) {
        let year = date.getFullYear();
        let moth = date.getMonth();
        let day = date.getDate();
        return year + "-" + moth + "-" + day;
    }
    /**
     * 获取当天指定时间的时间戳
     * @param hour 时
     * @param minute 分
     * @param second 秒
     * @example
     * const time = TimeUtil.getTargetTimestamp(10, 20, 30); // 1601259630000
     * const timeString = new Date(time).toLocaleString(); // "上午10:20:30"
     */
    public static getTargetTimestamp(hour: number = 0, minute: number = 0, second: number = 0): number {
        const start = new Date(new Date().toLocaleDateString()).getTime();
        const target = ((hour * 3600) + (minute * 60) + second) * 1000;
        return new Date(start + target).getTime();
    }

    /**
     * 将毫秒转为时分秒的格式（最小单位为秒，如：”00:01:59“）
     * @param time 毫秒数
     * @param separator 分隔符
     * @param keepHours 当小时数为 0 时依然展示小时数
     * @example 
     * const HMS = TimeUtil.msToHMS(119000); // "00:01:59"
     */
    public static msToHMS(time: number, separator: string = ':', keepHours: boolean = true): string {
        const hours = Math.floor(time / 3600000);
        const minutes = Math.floor((time - (hours * 3600000)) / 60000);
        const seconds = Math.floor((time - (hours * 3600000) - (minutes * 60000)) / 1000);
        const hoursString = (hours === 0 && !keepHours) ? '' : hours.toString().padStart(2, '0');
        return `${hoursString}${separator}${minutes.toString().padStart(2, '0')}${separator}${seconds.toString().padStart(2, '0')}`;
    }

    public static createUUID() {
        var d = new Date().getTime();
        var uuid = 'xxxxxxxx-yyyy-xxxx-yyyy-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
        return uuid;
    };
}
